﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Intranet.Controllers
{
    public class ActivitiesController : IntranetController
    {
        public ActionResult MyActivities()
        {
            // get all activities for logged in user
            // user not found?  then create them

            Guid myUserId = base.RegisterOrAuthenticateUser(User.Identity);

            using (Models.intranetDBEntities entities = new Models.intranetDBEntities())
            {
                var myActivities = entities.activities.Where(p => p.assignedTo == myUserId);
                return PartialView(myActivities.ToList());
            }
        }

        //
        // GET: /Activities/Create

        public ActionResult NewActivity()
        {
            Models.activity activity = new Models.activity();
            activity.assignedTo = base.RegisterOrAuthenticateUser(User.Identity);
            return View(activity);
        } 

        //
        // POST: /Activities/Create

        [HttpPost]
        public ActionResult NewActivity(Models.activity collection)
        {
            try
            {
                // TODO: Add insert logic here
                collection.createdDate = DateTime.Now;
                collection.details = "No details added yet";
                collection.dueDate = DateTime.Now.AddDays(1);
                collection.owner = collection.assignedTo;
                using (Models.intranetDBEntities entities = new Models.intranetDBEntities())
                {
                    entities.activities.AddObject(collection);
                    entities.SaveChanges();
                }
                return RedirectToAction("MyActivities");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Activities/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Activities/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Activities/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Activities/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
